<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Register</title>
  <script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
  <!-- 导入validate插件 -->
  <script type="text/javascript" src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
  <script type="text/javascript" src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/localization/messages_zh.js"></script>
  <script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/localization/messages_zh.js"></script>
  <link rel="stylesheet" type="text/css" href="../css/register.css">
  <script type="text/javascript" src="../js/register.js"></script>
  <meta id="viewport" name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1,initial-scale=1,user-scalable=no">
  <script src="https://cdn1.lncld.net/static/js/av-min-1.2.1.js"></script>
  <script type="text/javascript" src="../js/utils.js"></script>

</head>
<body>
<div class="register">
  <form id="Myform">
    <div>
      <div>
        <div class="register-vertification">
          <div id="number">
               <img id="img-phone" src="../images/phone.jpg">
             <input class="vertification" id="phonenumber" type="text" name="phonenumber" placeholder="中国大陆手机号" style="background:white" >
             <label id="get-vertification">获取验证码</label>
          </div>
          <!-- <div
          <input type="text" id="getnumber" name="number" placeholder="获取验证码" >
          </div> -->
            <div id="vertification-number">
                <img id="img-key" src="../images/key.jpg">
                <input type="text" class="vertification" name="vertificationnumber" id="vertificationnumber" placeholder="输入验证码" >
          </div>
        </div>
        <div class="hint">
          <div id="agree">绑定账号行为表示您同意</div>
          <div id="explain">《隐私说明》</div>
        </div>
      </div>
      <div id="button">
        <button id="register-button"  type="button">登 录</button>
      </div>
    </div>
  </form>
  <div>
    <label id="errormessage"></label>
  </div>
</div>

  <script>
    $(document).ready(function() {

      var APP_ID = 'Gm6RGzfleHPVMWOxgL8XYG19-gzGzoHsz';
      var APP_KEY = 'e4yNVE3KJUEEAqFMsdTC8NM8';

      AV.init({
        appId: APP_ID,
        appKey: APP_KEY
      });


      // try{
      //       responesToPhone(true)
      //     } catch(error){
      //       console.log("捕捉到错误")
      //     }
      var TestObject = AV.Object.extend('TestObject');
      var testObject = new TestObject();
      testObject.save({
        words: 'Hello World!'
      }).then(function(object) {
        // try{
        //     responesToPhone(true)
        //   } catch(error){
        //     console.log("捕捉到错误")
        //   }
      })

      // var currentUser = AV.User.current();

      // alert(currentUser.objectId)
      // if (currentUser) {
      //     alert("已登录")
      //     try{
      //       responesToPhone(true)
      //     } catch(error){
      //       console.log("捕捉到错误")
      //     }
      // } else {
      //   alert("未登陆")
      // }

      var countdown = 60
      var vLabel = $('#get-vertification')
      $('#get-vertification').bind("click", touch)

      function settime() {
        if (countdown == 0) {
          vLabel.attr("disabled", "false");
          vLabel.html("获取验证码");
          $('#get-vertification').bind("click", touch)
          countdown = 60;
          return
        } else {
          vLabel.attr("disabled", true);
          vLabel.html("重新发送(" + countdown + ")");
          countdown--;
        }
        $('#get-vertification').unbind("click", touch)
        setTimeout(function() {
          settime(vLabel)
        }, 1000)

      }

      function touch() {
        if (!$('#phonenumber').val() == "") {
          settime()
          AV.Cloud.requestSmsCode($('#phonenumber')[0].value).then(function(success) {
            // alert(success)
          }, function(error) {
            alert(error)
          });
        }

      }
      $('#register-button').click(function() {

        // AV.User.signUpOrlogInWithMobilePhone($('#phonenumber')[0].value, $("#vertificationnumber")[0].value).then(function(success) {

          var dataInfo = {
            phonenumber: $('#phonenumber')[0].value
          }

          AjaxIO.post("/saveInBackground", {
              phonenumber: $('#phonenumber')[0].value
            },
            function(result) {

              if (result.data.token) {
                localStorage.setItem("token", result.data.token);

              }
              localStorage.setItem("phonenumber",$('#phonenumber')[0].value)
              try {
                responesToPhone(true)

              } catch(error) {
                console.log("捕捉错误")
              }
            });

        // }, function(error) {
        //   // 失败
        //   alert("失败")
        // });
      })
    })

    function responesToPhone(isSuccess) {
      // alert("运行了")
      window.webkit.messageHandlers.responesToPhone.postMessage(isSuccess);
    }

  </script>
</body>
</html>